package com.xiaoniu.imclient.networkModule;

import android.util.Log;

import com.xiaoniu.lib_keleim.Schemes;
import com.xiaoniu.lib_keleim.pojo.vos.PreSendMessage;
import com.xiaoniu.lib_keleim.pojo.vos.UserInfoVO;
import com.xiaoniu.lib_keleim.spi.INetworkModule;
import com.xiaoniu.lib_keleim.spi.INetworkModuleFactory;
import com.xiaoniu.lib_keleim.spi.ReceiveMessageListener;
import com.xiaoniu.lib_keleim.utils.LogUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

public class WebSocketNetWorkModuleFactory implements INetworkModuleFactory {

    @Override
    public Schemes getSupportedUriSchemes() {
        return Schemes.WEB_SOCKET;
    }

    @Override
    public INetworkModule createNetworkModule() {
        return new WebSocketNetworkModule();
    }


    public static class  WebSocketNetworkModule extends WrapperNetworkModule  {
        private OkHttpClient CLIENT;
        private WebSocket mWebSocket;
        boolean isConnect = false;
        byte mRetryCount = 0;
        ReceiveMessageListener mReceiveMessageListener;

        String URL = "ws://192.168.0.101:8098?customerId=%s&timestamp=%s";

        private WebSocketNetworkModule() {
            CLIENT = new OkHttpClient.Builder()
                    .writeTimeout(5, TimeUnit.SECONDS)
                    .readTimeout(5, TimeUnit.SECONDS)
                    .connectTimeout(5, TimeUnit.SECONDS)
                    .build();
        }

        @Override
        public void connect(UserInfoVO userInfo) {
            if (mWebSocket != null) {
                mWebSocket.cancel();
            }
            String url = String.format(URL,userInfo.customerId,System.currentTimeMillis());
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            mWebSocket = CLIENT.newWebSocket(request, new WebSocketListener() {
                @Override
                public void onOpen(WebSocket webSocket, Response response) {
                    super.onOpen(webSocket, response);
                    isConnect = true;
                    mRetryCount = 0;
                    LogUtils.d("连接成功 "+ response);
                }

                @Override
                public void onMessage(WebSocket webSocket, String text) {
                    super.onMessage(webSocket, text);
                    LogUtils.d("收到消息 "+ text);

                    if (mReceiveMessageListener!=null) {
                        mReceiveMessageListener.onReceivedMessage("",text);
                    }
                }

                @Override
                public void onClosing(WebSocket webSocket, int code, String reason) {
                    super.onClosing(webSocket, code, reason);
                    LogUtils.d("连接关闭中 code "+ code + " reason "+ code);
                    isConnect = false;
                }

                @Override
                public void onClosed(WebSocket webSocket, int code, String reason) {
                    super.onClosed(webSocket, code, reason);
                    LogUtils.d("连接关闭 code "+ code + " reason "+ code);
                    isConnect = false;
                    retry();
                }

                @Override
                public void onFailure(WebSocket webSocket, Throwable t, Response response) {
                    super.onFailure(webSocket, t, response);
                    LogUtils.d("连接失败 t=" + t.getMessage());
                    isConnect = false;

                    retry();
                }

                private void retry() {

                }
            });
        }


        @Override
        public void sendMessage(PreSendMessage message) {
            LogUtils.d("发送消息 "+ message);
            mWebSocket.send(message.rawMessage);
        }

        @Override
        public void addOnReceiveMessageListener(ReceiveMessageListener receiveMessageListener) {
            this.mReceiveMessageListener = receiveMessageListener;
        }

    }
}
